package xml.uidElements.output;

import java.util.ArrayList;
import java.util.List;

import xml.others.IdManager;
import xml.others.UIDException;
import xml.uidElements.TransitionFromSet;
import xml.validations.FinderOutputElements;
import xml.validations.StructuresValidator;

public class OutputStructureSet extends AbstractOutputStructure {

	protected String id;
	protected String name;
	protected List<DataOutputElement> listDataItemsAndStructures;
	private int cardinalityMin;
	private int cardinalityMax;
	private boolean optional;
	private List<TransitionFromSet> listOutgoingTransitions;

	public OutputStructureSet(String name, String data) {
		this.id = IdManager.getInstance().getId(IdManager.OUTPUTSTRUCTURESET);
		this.name = name;

		if (new StructuresValidator().isValid(data)) {
			listDataItemsAndStructures = new FinderOutputElements().findOutput(data);

		}
	}

	public void setCardinality(int cardinalityMin, int cardinalityMax) {
		this.setCardinalityMin(cardinalityMin);
		this.setCardinalityMax(cardinalityMax);

	}

	public void setCardinalityMax(int cardinalityMax) {
		this.cardinalityMax = cardinalityMax;
	}

	public int getCardinalityMax() {
		return cardinalityMax;
	}

	public void setCardinalityMin(int cardinalityMin) {
		this.cardinalityMin = cardinalityMin;
	}

	public int getCardinalityMin() {
		return cardinalityMin;
	}

	@Override
	public String getId() {
		return id;
	}
	
	@Override
	public String getName() {
		return name;
	}

	public void addDataOutput(DataOutputElement d) {
		if (listDataItemsAndStructures == null) {
			listDataItemsAndStructures = new ArrayList<DataOutputElement>();
		}
		listDataItemsAndStructures.add(d);

	}

	public void setOptional(boolean optional) {
		this.optional = optional;
	}

	public boolean isOptional() {
		return optional;
	}

	public void insertOutgoingTransition(TransitionFromSet transition) {

		if (listOutgoingTransitions == null)
			listOutgoingTransitions = new ArrayList<TransitionFromSet>();

		for (int i = 0; i < this.listOutgoingTransitions.size(); i++) {
			TransitionFromSet trans = this.listOutgoingTransitions.get(i);
			if (trans.getId().equals(transition.getId()))
				throw new UIDException(UIDException.EXISTING_TRANSITION,
						"There is already a transition with this id");
		}
		listOutgoingTransitions.add(transition);

	}

}